package com.cxh.rmsq.ui.activity.self

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cxh.rmsq.R
import com.cxh.rmsq.common.IntentExtra
import com.cxh.rmsq.db.model.MineBean
import com.cxh.rmsq.db.model.UserInfo
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.model.Resource2
import com.cxh.rmsq.viewmodel.UserInfoViewModel
import com.google.android.flexbox.FlexboxLayoutManager
import com.youth.banner.Banner


/**
 * 查看好友信息
 */
//class LookUserDetailActivity : BaseActivity<AllViewModel, ActivityLookUserDetailBinding>(),
class LookUserDetailActivity : com.cxh.rmsq.ui.BaseActivity(),
    View.OnClickListener {

    private val clToolbar: LinearLayout by lazy { findViewById<LinearLayout>(R.id.clToolbar) }
    private val ivBack: LinearLayout by lazy { findViewById<LinearLayout>(R.id.iv_back) }
    private val ivRightImage: ImageView by lazy { findViewById<ImageView>(R.id.iv_right_image) }
    val banner: Banner<*, *> by lazy { findViewById<Banner<*, *>>(R.id.banner) }
    private val tvName: TextView by lazy { findViewById<TextView>(R.id.tv_name) }
    private val tvSexAge: TextView by lazy { findViewById<TextView>(R.id.tv_sex_age) }
    private val tvFun: TextView by lazy { findViewById<TextView>(R.id.tv_fun) }
    private val tvZan: TextView by lazy { findViewById<TextView>(R.id.tv_zan) }
    private val tvCircleCount: TextView by lazy { findViewById<TextView>(R.id.tv_circleCount) }
    private val tvIp: RecyclerView by lazy { findViewById<RecyclerView>(R.id.tv_ip) }
    private val tvNone: TextView by lazy { findViewById<TextView>(R.id.tv_none) }
    private val stvSend: TextView by lazy { findViewById<TextView>(R.id.stv_send) }
    private val stvAddBlack: TextView by lazy { findViewById<TextView>(R.id.stv_add_black) }

    var targetId = ""
    var targetName = ""
    private val latestUserInfo: UserInfo? = null
    private var banner_data = mutableListOf<String>()
    private var userInfoViewModel: UserInfoViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_look_user_detail)
        initView()
        initData()
    }

     fun initView() {
        clToolbar.setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent))
        BarUtils.setStatusBarLightMode(this, true)
        targetId = intent.getStringExtra(IntentExtra.STR_TARGET_ID).toString()
        ivBack.setOnClickListener(this)
        ivRightImage.setOnClickListener(this)

        stvSend.setOnClickListener(this)
        stvAddBlack.setOnClickListener(this)

    }

     fun initData() {
        userInfoViewModel = ViewModelProviders.of(this).get(UserInfoViewModel::class.java)
        userInfoViewModel!!.requestUserSelfInfo(IMManager.getInstance().currentId)
        userInfoViewModel!!
            .userSelfInfo
            .observe(
                this,
                object : Observer<Resource2<MineBean?>?> {
                    override fun onChanged(it: Resource2<MineBean?>?) {
                        if (it!!.data != null) {
                            tvName.setText(it.data?.nickname)
                            tvSexAge.setText(it.data?.age.toString())
                            tvFun.setText(it.data?.followerCount.toString())
                            tvZan.setText(it.data?.likedCount.toString())
                            tvCircleCount.setText(it.data?.circleCount.toString())
                            if (1 == it.data?.sex) {
                                val drawable = resources.getDrawable(R.mipmap.icon_male)
                                tvSexAge.setCompoundDrawablesWithIntrinsicBounds(
                                    drawable,
                                    null,
                                    null,
                                    null
                                )
                                tvSexAge.setBackgroundResource(R.drawable.shape_sex_male_bg)
                            } else {
                                val drawable = resources.getDrawable(R.mipmap.icon_male)
                                tvSexAge.setCompoundDrawablesWithIntrinsicBounds(
                                    drawable,
                                    null,
                                    null,
                                    null
                                )
                                tvSexAge.setBackgroundResource(R.drawable.shape_sex_female_bg)
                            }
                            var list = mutableListOf<String>()
                            list.add(it.data?.age.toString() + "岁")

                            if (!it.data?.constellation.isNullOrBlank()) {
                                list.add(it.data?.constellation.toString())
                            }
                            if (!it.data?.address.isNullOrBlank()) {
                                list.add("现居" + it.data?.address)
                            }
                            if (!it.data?.mbti.isNullOrBlank()) {
                                list.add(it.data?.mbti.toString())
                            }
                            if (!it.data?.highSchool.isNullOrBlank()) {
                                list.add(it.data?.highSchool.toString())
                            }
                            if (!it.data?.industry.isNullOrBlank()) {
                                list.add(it.data?.industry.toString())
                            }
                            tvIp.layoutManager = FlexboxLayoutManager(applicationContext)
                            tvIp.adapter = TagAdapter(list, list.size > 3)


                            if (it.data?.photoList != null && it.data.photoList.size > 0) {
                                banner_data.addAll(it.data.photoList)
                            } else {
                                banner_data.add(it.data?.avatar!!)
                            }

//                            banner.setAdapter(object :
//                                BannerImageAdapter<String>(banner_data) {
//                                override fun onBindView(
//                                    holder: BannerImageHolder,
//                                    data: String,
//                                    position: Int,
//                                    size: Int
//                                ) {
//                                    //                        holder.imageView.setImageResource(data)
//                                    loadImage(data, holder.imageView)
//                                }
//                            })
//
//                            // 开启循环轮播
//                            banner.isAutoLoop(true)
//                            banner.setIndicator(CircleIndicator(applicationContext))
//                            //binding.tab1Banner.setScrollBarFadeDuration(3000)
//                            // 设置指示器颜色
//                            banner.setIndicatorSelectedColor(resources.getColor(R.color.white))
//                            banner.setIndicatorNormalColor(resources.getColor(R.color.color_FF6300))
//                            // 开始轮播
//                            banner.start()
                        }
                    }
                })
//        viewModel.getUserInfo(targetId).observe(this) {
//            if (it.code == 200 ) {
////                mBinding.profileTvDetailDisplayName.text = it.data!!.nickname
////                targetName = it.data!!.nickname.toString()
////                loadImage(it.data.avatar, mBinding.profileIvDetailUserPortrait)
//
//                mBinding.tvName.setText(it.data?.nickname)
//                mBinding.tvSexAge.setText(it.data?.age.toString())
//                mBinding.tvFun.setText(it.data?.followerCount.toString())
//                mBinding.tvZan.setText(it.data?.likedCount.toString())
//                mBinding.tvCircleCount.setText(it.data?.circleCount.toString())
//                if (1 == it.data?.sex) {
//                    val drawable = resources.getDrawable(R.mipmap.icon_male)
//                    mBinding.tvSexAge.setCompoundDrawablesWithIntrinsicBounds(
//                        drawable,
//                        null,
//                        null,
//                        null
//                    )
//                    mBinding.tvSexAge.setBackgroundResource(R.drawable.shape_sex_male_bg)
//                } else {
//                    val drawable = resources.getDrawable(R.mipmap.icon_male)
//                    mBinding.tvSexAge.setCompoundDrawablesWithIntrinsicBounds(
//                        drawable,
//                        null,
//                        null,
//                        null
//                    )
//                    mBinding.tvSexAge.setBackgroundResource(R.drawable.shape_sex_female_bg)
//                }
//                var list = mutableListOf<String>()
//                list.add(it.data?.age.toString() + "岁")
//
//                if (!it.data?.constellation.isNullOrBlank()) {
//                    list.add(it.data?.constellation.toString())
//                }
//                if (!it.data?.address.isNullOrBlank()) {
//                    list.add("现居" + it.data?.address)
//                }
//                if (!it.data?.mbti.isNullOrBlank()) {
//                    list.add(it.data?.mbti.toString())
//                }
//                if (!it.data?.highSchool.isNullOrBlank()) {
//                    list.add(it.data?.highSchool.toString())
//                }
//                if (!it.data?.industry.isNullOrBlank()) {
//                    list.add(it.data?.industry.toString())
//                }
//                mBinding.tvIp.layoutManager = FlexboxLayoutManager(this)
//                mBinding.tvIp.adapter = TagAdapter(list, list.size > 3)
//
//
//                if (it.data?.photoList != null && it.data.photoList.size > 0) {
//                    banner_data.addAll(it.data.photoList)
//                } else {
//                    banner_data.add(it.data?.avatar!!)
//                }
//
//                mBinding.banner.setAdapter(object : BannerImageAdapter<String>(banner_data) {
//                    override fun onBindView(
//                        holder: BannerImageHolder,
//                        data: String,
//                        position: Int,
//                        size: Int
//                    ) {
////                        holder.imageView.setImageResource(data)
//                        loadImage(data, holder.imageView)
//                    }
//                })
//
//                // 开启循环轮播
//                mBinding.banner.isAutoLoop(true)
//                mBinding.banner.setIndicator(CircleIndicator(this))
//                //binding.tab1Banner.setScrollBarFadeDuration(3000)
//                // 设置指示器颜色
//                mBinding.banner.setIndicatorSelectedColor(resources.getColor(R.color.white))
//                mBinding.banner.setIndicatorNormalColor(resources.getColor(R.color.color_FF6300))
//                // 开始轮播
//                mBinding.banner.start()
//            }else {
//                showCustomizeToast(it.msg)
//                finish()
//            }
//        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> {
                finish()
            }

            R.id.iv_right_image -> {

            }

            R.id.stv_send -> {

            }

            R.id.stv_add_black -> {

            }

        }
    }

    inner class TagAdapter(list: MutableList<String>, boo: Boolean) :
        BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_tag_dict, list) {
        var list = list
        var boo = boo
        override fun convert(holder: BaseViewHolder, item: String) {
            if (list.size == 1) {
                holder.getView<TextView>(R.id.stv_type)
                    .setTextColor(resources.getColor(R.color.color_333333))
                holder.getView<TextView>(R.id.stv_type).background =
                    resources.getDrawable(R.drawable.shape_tab1_6)
            } else if (list.size == 2) {
                when (holder.layoutPosition) {
                    0 -> {
                        holder.getView<TextView>(R.id.stv_type)
                            .setTextColor(resources.getColor(R.color.color_333333))
                        holder.getView<TextView>(R.id.stv_type).background =
                            resources.getDrawable(R.drawable.shape_tab1_6)
                    }

                    1 -> {
                        holder.getView<TextView>(R.id.stv_type)
                            .setTextColor(resources.getColor(R.color.color_333333))
                        holder.getView<TextView>(R.id.stv_type).background =
                            resources.getDrawable(R.drawable.shape_tab2_6)
                    }
                }
            } else if (list.size == 3) {
                when (holder.layoutPosition) {
                    0 -> {
                        holder.getView<TextView>(R.id.stv_type)
                            .setTextColor(resources.getColor(R.color.color_333333))
                        holder.getView<TextView>(R.id.stv_type).background =
                            resources.getDrawable(R.drawable.shape_tab1_6)
                    }

                    1 -> {
                        holder.getView<TextView>(R.id.stv_type)
                            .setTextColor(resources.getColor(R.color.color_333333))
                        holder.getView<TextView>(R.id.stv_type).background =
                            resources.getDrawable(R.drawable.shape_tab2_6)
                    }

                    2 -> {
                        holder.getView<TextView>(R.id.stv_type)
                            .setTextColor(resources.getColor(R.color.color_333333))
                        holder.getView<TextView>(R.id.stv_type).background =
                            resources.getDrawable(R.drawable.shape_tab3_6)
                    }

                }
            } else {
                if (holder.layoutPosition == 0) {
                    holder.getView<TextView>(R.id.stv_type)
                        .setTextColor(resources.getColor(R.color.color_333333))
                    holder.getView<TextView>(R.id.stv_type).background =
                        resources.getDrawable(R.drawable.shape_tab1_6)
                } else if (holder.layoutPosition == 1) {
                    holder.getView<TextView>(R.id.stv_type)
                        .setTextColor(resources.getColor(R.color.color_333333))
                    holder.getView<TextView>(R.id.stv_type).background =
                        resources.getDrawable(R.drawable.shape_tab2_6)
                } else if (holder.layoutPosition == 2) {
                    holder.getView<TextView>(R.id.stv_type)
                        .setTextColor(resources.getColor(R.color.color_333333))
                    holder.getView<TextView>(R.id.stv_type).background =
                        resources.getDrawable(R.drawable.shape_tab3_6)
                } else {
                    holder.getView<TextView>(R.id.stv_type)
                        .setTextColor(resources.getColor(R.color.color_333333))
                    holder.getView<TextView>(R.id.stv_type).background =
                        resources.getDrawable(R.drawable.shape_tab4_6)
                }
                if (holder.layoutPosition == list.size - 1) {
                    holder.getView<TextView>(R.id.stv_type)
                        .setTextColor(resources.getColor(R.color.color_333333))
                    holder.getView<TextView>(R.id.stv_type).background =
                        resources.getDrawable(R.drawable.shape_tab5_6)
                }
            }

//            if (boo) {
//                if (holder.layoutPosition == 3) {
//                    holder.getView<TextView>(R.id.stv_type)
//                        .setTextColor(resources.getColor(R.color.color_AF74DE))
//                    holder.getView<TextView>(R.id.stv_type).background =
//                        resources.getDrawable(R.drawable.shape_tab4_6)
//                }
//            }

            holder.setText(R.id.stv_type, item)

        }
    }
}